//
//  ZGLOrderPayController.m
//  ZeegingLottery
//
//  Created by 李曌 on 2018/7/3.
//  Copyright © 2018年 蒂埃里. All rights reserved.
//

#import "ZGLOrderPayController.h"
#import "ZGLOrderPayCell.h"

@interface ZGLOrderPayController ()
<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic, strong)NSArray *payTypeNameArr;
@property (nonatomic, strong)NSArray *payTypeImageArr;
@property (nonatomic, strong)UILabel *totalMoneyLabel;
@property (nonatomic, strong)UITableView *PayWayTableView;
@property (nonatomic, strong)UIButton *confirmPayBtn;

@property (nonatomic, strong) NSIndexPath *lastSelected;  //上一次选中的
@property (nonatomic, assign) NSInteger selectedTag;
@property (nonatomic, copy) NSString *balance;//余额
@property (nonatomic, copy) NSString *order_no;
@property (nonatomic, copy) NSString *totalMoney; //支付总金额

@property (nonatomic, copy) NSString *wechat_pay;
@property (nonatomic, copy) NSString *ali_pay;
@property (nonatomic, copy) NSString *balance_pay;

@end

@implementation ZGLOrderPayController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"订单支付";
    self.view.backgroundColor = ViewBackgroundColor;
    [self setupTableView];
    

    _payTypeNameArr = @[@"余额支付",@"支付宝",@"微信支付"];
    _payTypeImageArr = @[@"icon_charge_phonebill",@"icon_charge_alipay",@"icon_charge_wechat"];
    
    
//    self.wechat_pay = @"0";
//    self.ali_pay = @"0";
//    self.balance_pay = @"0";
}


- (void)setOrderInfoDict:(NSDictionary *)orderInfoDict
{
    _orderInfoDict = orderInfoDict;
    DLog(@"orderInfoDict--%@",orderInfoDict);
    
    self.order_no = orderInfoDict[@"order_no"];
    self.totalMoney = orderInfoDict[@"amount"];

    //取支付支持状态
    NSString *path = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)lastObject];
    NSString *fileName = [path stringByAppendingPathComponent:@"Paylist.plist"];
    NSDictionary *paymentList = [NSDictionary dictionaryWithContentsOfFile:fileName];
    self.wechat_pay = paymentList[@"wechat_pay"];
    self.ali_pay = paymentList[@"ali_pay"];
    self.balance_pay = paymentList[@"balance"];
    DLog(@"paymentList-%@",paymentList);
    
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 3;

}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    ZGLOrderPayCell *cell = [ZGLOrderPayCell orderPayCellWith:tableView];
    
    //传入支付金额和余额
    self.balance = sZGLUserInfoModel.balance;
    NSString *total = _totalMoney;
    [cell comparTotalMoney:total Balance:self.balance];
    cell.ali_pay = self.ali_pay;
    cell.weChat_pay = self.wechat_pay;
    [cell setNameWith:_payTypeNameArr[indexPath.row] Icon:_payTypeImageArr[indexPath.row]];

    if ([self.totalMoney intValue] < [self.balance intValue]) {

        if (indexPath.row == 0) {
            [cell.selectedImage setHidden:NO];
            self.lastSelected = indexPath;
//            self.selectedTag = indexPath.row;
        }

    }else if ([self.ali_pay integerValue] == 1){

        if (indexPath.row == 1) {
            [cell.selectedImage setHidden:NO];
            self.lastSelected = indexPath;
//            self.selectedTag = indexPath.row;
        }


    }else if ([self.wechat_pay integerValue] == 1){

        if (indexPath.row == 2) {
            [cell.selectedImage setHidden:NO];
            self.lastSelected = indexPath;
//            self.selectedTag = indexPath.row;
        }

    }

    DLog(@"self.lastSelected--%ld",self.lastSelected.row);
    
    
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 50;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    //如果总金额>余额，余额支付就不能点击
    NSString *balance = self.balance; //余额
    
    if (indexPath.row == 0 && [self.totalMoney integerValue]>[balance integerValue]) {
        
        DLog(@"余额不足");
        
    }else if (indexPath.row == 1 && [self.ali_pay integerValue] == 0){
        
        DLog(@"支付宝支付不支持");

    }else if (indexPath.row == 2 && [self.wechat_pay integerValue] == 0){
        
        DLog(@"微信支付不支持");
 
    }else{
        
        if (self.lastSelected) {
            ZGLOrderPayCell *cell = [tableView cellForRowAtIndexPath:self.lastSelected];
            [cell.selectedImage setHidden:YES];
        }
        
        ZGLOrderPayCell *cell = [tableView cellForRowAtIndexPath:indexPath];
        [cell.selectedImage setHidden:NO];
        
        self.lastSelected = indexPath;
//        self.selectedTag = indexPath.row;
        
        DLog(@"%ld",self.lastSelected.row);
    }
    
}


#pragma mark - setupUI

- (void)setupTableView
{
    UILabel *totalMoneyLabel = [[UILabel alloc]init];
    totalMoneyLabel.textColor = mainTextColor;
    totalMoneyLabel.textAlignment = 1;
    totalMoneyLabel.font = [UIFont fontWithName:@"Helvetica-Bold" size:30];
//    NSString *amount = self.orderInfoDict[@"amount"];
    NSString *amount = self.totalMoney;
    totalMoneyLabel.text = [NSString stringWithFormat:@"￥%@",amount];
    _totalMoneyLabel = totalMoneyLabel;
    
    [self.view addSubview:self.totalMoneyLabel];
    [_totalMoneyLabel mas_makeConstraints:^(MASConstraintMaker *make) {

        make.left.equalTo(self.view).offset(10);
        make.top.equalTo(self.view).offset(30);
        make.right.equalTo(self.view).offset(-10);
    }];
    
    
    
    UITableView *PayWayTableView = [[UITableView alloc]init];
    PayWayTableView.delegate = self;
    PayWayTableView.dataSource = self;
    PayWayTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    PayWayTableView.backgroundColor = [UIColor whiteColor];
    [PayWayTableView setScrollEnabled:NO];
    _PayWayTableView = PayWayTableView;
    [self.view addSubview:self.PayWayTableView];
    [_PayWayTableView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.equalTo(self.view);
        make.top.equalTo(_totalMoneyLabel.mas_bottom).offset(30);
        make.right.equalTo(self.view);
        make.height.equalTo(@(150));
    }];
    
    UIButton *confirmPayBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [confirmPayBtn setTitle:@"确认支付" forState:UIControlStateNormal];
    [confirmPayBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [confirmPayBtn setBackgroundColor:sRGBHexAlpha(0xF63F3F, 1.0)];
    confirmPayBtn.titleLabel.font = [UIFont systemFontOfSize:17];
    confirmPayBtn.layer.cornerRadius = 5;
    confirmPayBtn.layer.masksToBounds = YES;
    [confirmPayBtn addTarget:self action:@selector(confirmPayBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    _confirmPayBtn = confirmPayBtn;
    [self.view addSubview:self.confirmPayBtn];
    [self.confirmPayBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.equalTo(_PayWayTableView.mas_bottom).offset(30);
        make.left.equalTo(self.view).offset(20);
        make.right.equalTo(self.view).offset(-20);
        make.height.equalTo(@(50));
        
    }];
}



#pragma mark - method

//确认支付
- (void)confirmPayBtnClick:(UIButton *)sender
{
    //菊花
    [EasyLoadingView showLoadingText:@"" config:^EasyLoadingConfig *{
        
        return [EasyLoadingConfig shared].setLoadingType(LoadingShowTypeIndicator);
    }];
    
    DLog(@"确认支付");
    WS(weakself);
    NSInteger selectedRow = self.lastSelected.row;
    NSInteger balancePay = [self.balance_pay integerValue];
    if (selectedRow == 0 && balancePay == 1) {
        
        DLog(@"余额支付");
        [sZGLHTTPRequestManager useBalancePayWithOrderNum:self.order_no Success:^(id responsedata) {
            
            DLog(@"responsedata--%@",responsedata);
            NSString *code = responsedata[@"code"];
            NSString *message = responsedata[@"message"];
            NSDictionary *data = responsedata[@"data"];
            if ([code integerValue] == 1) {
                DLog(@"支付成功");
                
                [EasyLoadingView hidenLoading];
                [EasyTextView showSuccessText:@"支付成功"];
                NSString *url = data[@"url"];
                ZGLSuccessCallBackVC *callBackVc = [[ZGLSuccessCallBackVC alloc]init];
                callBackVc.url = url;
                [callBackVc initWithCallBackStatus:ZGLBusinessCallBackPay Status:ZGLStatusSuccess];
                
                [weakSelf cw_pushViewController:callBackVc drewerHiddenDuration:0.01];
                
                //成功通知
                [[NSNotificationCenter defaultCenter] postNotificationName:ZGLPaySuccessNotifi object:nil];
                
            }else if ([code integerValue] == 0  ) {
                
                DLog(@"支付失败");
                [EasyLoadingView hidenLoading];
                ZGLSuccessCallBackVC *failCallBackVC = [[ZGLSuccessCallBackVC alloc]init];
                [failCallBackVC initWithCallBackStatus:ZGLBusinessCallBackPay Status:ZGLStatusFail];
                failCallBackVC.subMessageLabel.text = message;
                
                [weakSelf cw_pushViewController:failCallBackVC drewerHiddenDuration:0.01];
            }
            
        } Fail:^(ZGLRespondStatusCode bef_status, NSString *bef_message, NSError *error) {
            
            DLog(@"支付失败--%@",error);
            [EasyLoadingView hidenLoading];
        }];
        
        
    }else if (self.lastSelected.row == 1){
        
        DLog(@"支付宝支付");
        
        
        
    }else if (self.lastSelected.row == 2){
        
        DLog(@"微信支付");
        
        
        
    }
    
}

@end
